//--------------------------------------------------------------------------------------
// register the function
 var newRegistery1 = new Array("getPlayerMainViewData", "drawPlayerTable" ); 
 allJsonsRegistry = allJsonsRegistry.concat(newRegistery1);

//--------------------------------------------------------------------------------------
// implement the function
// fill the table displaying the set percentages in the mtm repartition. the current mtm repartition (which coul be different
// from the set percentages because the market could have moved. and a column to set new percentages.
function drawPlayerTable(playerTableInfoDictionary) 
{
    myTable = document.getElementById('playerTable_div');

    //clean the table
    for(var i = myTable.rows.length; i > 0;i--)
    {
        myTable.deleteRow(i - 1);
    }
    // add the header
    var headerRow = myTable.insertRow(0);
    var newCell = headerRow.insertCell(0);
    newCell.innerHTML = '<th ><div class="myBold">Asset</div></th>';
    newCell = headerRow.insertCell(1);
    newCell.innerHTML = '<th ><div class="myBold">Number perc</div></th>';
    newCell = headerRow.insertCell(2);
    newCell.innerHTML = '<th ><div class="myBold">Current MtM perc</div></th>';
    newCell = headerRow.insertCell(3);
    newCell.innerHTML = '<th ><div class="myBold">No of assets</div></th>';
    newCell = headerRow.insertCell(4);
    newCell.innerHTML = '<th ><div class="myBold">Market price</div></th>';
    newCell = headerRow.insertCell(5);
    newCell.innerHTML = '<th ><div class="myBold">Value (c$)</div></th>';
    newCell = headerRow.insertCell(6);
    newCell.innerHTML = '<th ><div class="myBold">Set new perc</div></th>';

    var totalMtM = 0;	
    var totalNBAssets = 0;		
    //add the values 
    var inputStrHelper ='\" type=\"text\" size=\"10%\"  onkeypress=\"ifEnter(this,event,submitNewRepartition);\" value=\"';
	for (var i = 0; i < playerTableInfoDictionary.assetsList.length; i++)
	{
        var newRow = myTable.insertRow(i+1);
        var newCell = newRow.insertCell(0);
        var name0 = playerTableInfoDictionary.assetsList[i].name;
        var rep0 = Math.round( playerTableInfoDictionary.assetsList[i].playerView*10000 )/100;
        newCell.innerHTML = name0;
        newCell = newRow.insertCell(1);
        newCell.innerHTML = rep0 ;
        newCell = newRow.insertCell(2);
        newCell.innerHTML = Math.round( playerTableInfoDictionary.assetsList[i].mtmPerc*10000 )/100;
        newCell = newRow.insertCell(3);
        var nbAsset = playerTableInfoDictionary.assetsList[i].quantity
        totalNBAssets += nbAsset
        newCell.innerHTML = Math.round( nbAsset*100 )/100;
        newCell = newRow.insertCell(4);
        newCell.innerHTML = Math.round( playerTableInfoDictionary.assetsList[i].marketPrice*100 )/100;
        newCell = newRow.insertCell(5);
        var value = playerTableInfoDictionary.assetsList[i].value
        newCell.innerHTML = Math.round( value*100 )/100;
        totalMtM += value;
        newCell = newRow.insertCell(6);
        var string0 = '<td> <input name=\"' + name0 + inputStrHelper + rep0 + '\"</input></td>';
        newCell.innerHTML = string0;
	}
	
	// add the line for the total
	var totalRow = myTable.insertRow(1);
	var newCell = totalRow.insertCell(0);
	newCell.innerHTML = '<div class="myBold">Total </div>';
    newCell = totalRow.insertCell(1);
    newCell.innerHTML = '<div class="myBold">100</div>';
    newCell = totalRow.insertCell(2);
    newCell.innerHTML = '<div class="myBold">100</div>';
    newCell = totalRow.insertCell(3);
    newCell.innerHTML = '<div class="myBold">'+ Math.round( totalNBAssets*100 )/100 + '</div>';
    newCell = totalRow.insertCell(4);
    newCell.innerHTML = '<div class="myBold">NA</div>';
    newCell = totalRow.insertCell(5);
    newCell.innerHTML = '<div class="myBold">'+ Math.round( totalMtM*100 )/100 + '</div>';
    newCell = totalRow.insertCell(6);
    newCell.innerHTML = '';
    return false;
};


//--------------------------------------------------------------------------------------
//submit a new repartition
function submitNewRepartition() 
{ 
    return submitWithoutReload( 
        '/SubmitNewRepartition/',
        $('#playerTable_div'),
        '#id_messageDivRep',
        'The transaction was perfomed successfully.'
    );
}


//--------------------------------------------------------------------------------------
//sel asset
function sellAsset() 
{ 
    return submitWithoutReload(
        '/sellAsset/',
        $('#sellAsset_id'),
        '#id_messageDivSell',
        'The transaction was perfomed successfully.'
    );
};

